package com.jetgege.smartbox.BoxSetting;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jetgege.smartbox.Base.App;
import com.jetgege.smartbox.Base.BaseActivity;
import com.jetgege.smartbox.Me.DevicePWDActivity;
import com.jetgege.smartbox.Model.UserModel;
import com.jetgege.smartbox.R;
import com.kaopiz.kprogresshud.KProgressHUD;

import butterknife.BindView;
import butterknife.OnClick;
import me.drakeet.materialdialog.MaterialDialog;

import static com.jetgege.smartbox.Tools.Common.scheduleDismiss;

public class FingerprintActivity extends BaseActivity {

    @BindView(R.id.fingerprint_toolbar)
    Toolbar fingerprintToolbar;
    @BindView(R.id.iv_FrigerIcon1)
    ImageView ivFrigerIcon1;
    @BindView(R.id.iv_FrigerIcon2)
    ImageView ivFrigerIcon2;
    @BindView(R.id.ll_FrigerEidtLL2)
    LinearLayout llFrigerEidtLL2;
    UserModel userModel;
    @BindView(R.id.bt_ModifyFriger1)
    Button btModifyFriger1;
    @BindView(R.id.bt_ModifyFriger2)
    Button btModifyFriger2;
    @BindView(R.id.bt_DeleteFriger1)
    Button btDeleteFriger1;
    @BindView(R.id.bt_DeleteFriger2)
    Button btDeleteFriger2;
    @BindView(R.id.bt_ResetDev)
    Button btResetDev;
    @BindView(R.id.bt_ResetDevPWD)
    Button btResetDevPWD;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_fingerprint;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initValue();
        eventHandle();
    }

    int position = 0;

    private void initValue() {
        fingerprintToolbar.setTitle("盒子");
        setSupportActionBar(fingerprintToolbar);
        // Navigation Icon 要設定在 setSupoortActionBar 才有作用
        // 否則會出現 back button
        fingerprintToolbar.setNavigationIcon(R.drawable.bar_back_btn);
        //获取用户
        position = getIntent().getIntExtra("Position", 0);
        userModel = App.mDevices.getUserModels().get(position);
        //用户为管理员显示恢复出厂设置
        if (userModel.getUserType() == 1)
        {
            btResetDevPWD.setVisibility(View.VISIBLE);
            btResetDev.setVisibility(View.VISIBLE);
        }
        fingerState();
    }

    private void eventHandle() {
        fingerprintToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            }
        });

    }

    //  添加编辑
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mbox_setting, menu);
        return true;
    }

    /**
     * 设置编辑状态
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(FingerprintActivity.this, EditBoxActivity.class);
        intent.putExtra("Position", position);
        startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        return true;
    }

    /**
     * 指纹对应UI显示
     */
    private void fingerState() {
        switch (userModel.getFrigerNum()) {
            case 0:
                ivFrigerIcon1.setImageResource(R.drawable.friger_icon_normal);
                ivFrigerIcon2.setImageResource(R.drawable.friger_icon_normal);
                btModifyFriger1.setText("新增");
                btModifyFriger2.setText("新增");
                btDeleteFriger1.setEnabled(false);
                btDeleteFriger2.setEnabled(false);
                btDeleteFriger1.setBackgroundResource(R.drawable.shape_corners_delete_btn_default);
                btDeleteFriger2.setBackgroundResource(R.drawable.shape_corners_delete_btn_default);
                break;
            case 1:
                ivFrigerIcon1.setImageResource(R.drawable.friger_icon);
                ivFrigerIcon2.setImageResource(R.drawable.friger_icon_normal);
                btModifyFriger1.setText("修改");
                btModifyFriger2.setText("新增");
                btDeleteFriger1.setEnabled(true);
                btDeleteFriger2.setEnabled(false);
                btDeleteFriger1.setBackgroundResource(R.drawable.delete_color_btn);
                btDeleteFriger2.setBackgroundResource(R.drawable.shape_corners_delete_btn_default);
                break;
            case 2:
                ivFrigerIcon1.setImageResource(R.drawable.friger_icon);
                ivFrigerIcon2.setImageResource(R.drawable.friger_icon);
                btModifyFriger1.setText("修改");
                btModifyFriger2.setText("修改");
                btDeleteFriger1.setEnabled(true);
                btDeleteFriger2.setEnabled(true);
                btDeleteFriger1.setBackgroundResource(R.drawable.delete_color_btn);
                btDeleteFriger2.setBackgroundResource(R.drawable.delete_color_btn);
                break;
        }
    }

    @OnClick({R.id.bt_ModifyFriger1, R.id.bt_DeleteFriger1, R.id.bt_ModifyFriger2, R.id.bt_DeleteFriger2, R.id.bt_ResetDev,R.id.bt_ResetDevPWD})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.bt_ModifyFriger1://修改指纹
                Intent intent1 = new Intent(FingerprintActivity.this, InputFingerprintActivity.class);
                intent1.setAction("FingerprintActivity");
                startActivity(intent1);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.bt_ModifyFriger2:
                Intent intent2 = new Intent(FingerprintActivity.this, InputFingerprintActivity.class);
                intent2.setAction("FingerprintActivity");
                startActivity(intent2);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.bt_DeleteFriger1://删除指纹
                deleteFriger();
                break;
            case R.id.bt_DeleteFriger2:
                deleteFriger();
                break;
            case R.id.bt_ResetDev:
                resetDevice();
                break;
            case R.id.bt_ResetDevPWD:
                startActivity(new Intent(FingerprintActivity.this,DevicePWDActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;

        }
    }

    /**
     * 删除指纹提示
     */
    private void deleteFriger() {

        final MaterialDialog mMaterialDialog = new MaterialDialog(this)
                .setTitle("提示")
                .setMessage("确定要删除当前指纹吗？");
        mMaterialDialog.setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMaterialDialog.dismiss();
                userModel.setFrigerNum(userModel.getFrigerNum() - 1);
                fingerState();
                deleteTip();
            }
        });
        mMaterialDialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMaterialDialog.dismiss();
            }
        });

        mMaterialDialog.show();
    }

    private void deleteTip() {
        TextView tv_Tips = new TextView(this);
        tv_Tips.setText("删除成功！");
        tv_Tips.setTextColor(Color.WHITE);
        KProgressHUD hud = KProgressHUD.create(this)
                .setCustomView(tv_Tips);
        scheduleDismiss(hud, 1000);
        hud.show();
    }

    /**
     * 恢复出厂设置提示
     */
    private void resetDevice() {

        final MaterialDialog mMaterialDialog = new MaterialDialog(this)
                .setTitle("提示")
                .setMessage("确定要恢复出厂设置吗？");
        mMaterialDialog.setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMaterialDialog.dismiss();
                App.mDevices.getUserModels().clear();//删除所有用户
                resetTip();
            }
        });
        mMaterialDialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMaterialDialog.dismiss();
            }
        });

        mMaterialDialog.show();
    }

    private void resetTip() {
        TextView tv_Tips = new TextView(this);
        tv_Tips.setText("恢复成功！");
        tv_Tips.setTextColor(Color.WHITE);
        final KProgressHUD hud = KProgressHUD.create(this)
                .setCustomView(tv_Tips);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                hud.dismiss();
                Intent logout =new Intent("com.jetgege.broadcast.logout");
                sendBroadcast(logout);
            }
        },1000);
        hud.show();
    }

}
